package com.slg.android.listenbook.biz.launch;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.home.MainActivity;
import com.slg.android.listenbook.biz.home.moudle.Channel;
import com.slg.android.listenbook.common.DataStore;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class LaunchActivity extends Activity implements Runnable {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_launch);

        new Thread(this).start();
    }


    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        AssetManager assets = getResources().getAssets();
        try {
            InputStream stream = assets.open("FMlistinterface.json");
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line=reader.readLine())!= null){
                sb.append(line);
            }
            reader.close();
            List<Channel> channels = Channel.parseData(sb.toString());
            DataStore.getInstance().setChannelList(channels);
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
